package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import entity.MonthlyCard;
import main.Main;
import util.Constants;
import util.Validator;

public class MonthlyExtendJPanel extends JPanel {
	private static final long serialVersionUID = 2543185054428597662L;

	public MonthlyExtendJPanel(String cardId){
        setLayout(null);
        initUI(cardId);
	}
	
	private void initUI(String cardId){
        JLabel lab1 = new JLabel("月数",SwingConstants.RIGHT);
        lab1.setFont(Constants.font);
        lab1.setBounds(100, 30, 60, 30);
        
        JTextField lab1input = new JTextField();
        Validator.bind(lab1input,Validator.NUM);
        lab1input.setBounds(165, 30, 120, 30);
        
        JButton btn = new JButton("打印续费账单");
		btn.setBounds(165, 90, 120, 30);
		btn.setFont(Constants.font);
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lab1inputStr = lab1input.getText().trim();				
				if (Validator.isEmpty(lab1inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入月数！");
					return;
				}

				MonthlyCard card = new MonthlyCard();
				card.id = cardId;
				card.month = Integer.valueOf(lab1inputStr);
				
		
				JDialog dlg = new JDialog(Main.frame, "月卡续费账单",true);
				Main.dlgStack.push(dlg);
//				dlg.setBounds(300, 200, 450, 500);
				dlg.setSize(450, 500);
				dlg.setLocationRelativeTo(null);
				dlg.add(new AddMonthlyExtendReceiptJPanel(card));
				dlg.validate();
		        dlg.addWindowListener(new WindowAdapter() {
		        	@Override
		            public void windowClosing(WindowEvent e) {
		        		if(Main.dlgStack.size() == 2){
		        			Main.dlgStack.pop().dispose();
		        		}
		            }
				});
		        dlg.setVisible(true);
			}
		});
		
		add(lab1);
		add(lab1input);
		add(btn);
	}
}
